package com.offcn.controller;

import com.offcn.bean.Blog;
import com.offcn.service.BlogService;
import com.offcn.utils.OAResult;
import com.offcn.utils.PageView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/blog")
public class BlogController {

    @Autowired
    private BlogService blogService;

    @RequestMapping("/{page}")
    public String forwardPage(@PathVariable("page") String page, Integer bid, Model model){
        model.addAttribute("bid",bid);
        return "blog/"+page;
    }
    @Value("${pageSize}")
    private Integer pageSize;

    @RequestMapping("/getAllBlogs")
    @ResponseBody
    public PageView<Blog> getAllBlogs(@RequestParam(defaultValue = "1") int currentPage){
        return blogService.getAllBlogs(currentPage,pageSize);

    }

    @RequestMapping("/blogDetailById")
    @ResponseBody
    public Blog blogDetailById(int bid){
        return blogService.blogDetailById(bid);
    }

    @RequestMapping("/deleteBlog")
    @ResponseBody
    public OAResult deleteBlog(int bid){
        return blogService.deleteBlog(bid);
    }

    @RequestMapping("/saveBlog")
    @ResponseBody
    public OAResult saveBlog(Blog blog, HttpSession session){
        return blogService.saveBlog(blog,session);
    }






}
